package com.bazinga.contact.controller;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import com.bazinga.contact.model.JobDetails;
import com.bazinga.contact.util.HibernateUtil;


public class JobDetailsManager extends HibernateUtil {

	public JobDetails add(JobDetails jobDetails) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(jobDetails);
		session.getTransaction().commit();
		return jobDetails;
	}
	public JobDetails delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		JobDetails jobDetails = (JobDetails) session.load(JobDetails.class, id);
		if(null != jobDetails) {
			session.delete(jobDetails);
		}
		session.getTransaction().commit();
		return jobDetails;
	}

	@SuppressWarnings("unchecked")
	public List<JobDetails> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<JobDetails> jobDetails = null;
		try {
			
			jobDetails = (List<JobDetails>)session.createQuery("from JobDetails").list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return jobDetails;
	}
	
	public JobDetails getById(Long id) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		JobDetails jobDetails = (JobDetails) session.createQuery("from JobDetails where jobid="+id).uniqueResult();
		return jobDetails;
	}
	
	public List<JobDetails> getByUserId(Long userid) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<JobDetails> jobDetails = null;
		try {
		 jobDetails = (List<JobDetails>) session.createQuery("from JobDetails where userid="+userid).list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return jobDetails;
	}
}
